<?php 
/**
 * User: Lijun
 * Date: 2017/7/4.
 * Time: 15:46.
 * Comment:服务指南
 */

namespace Admin\Controller;
use Think\Controller;

class ServerController extends Controller{
    public function index(){
        $server = M("server") -> select();
        $this -> assign("server",$server);
        $this -> display();
    }
    
    public function addServer(){
        $this -> display();
    }
    
    public function doAddServer(){
        $info["name"] = I("name");
        $info["url"] = I("url");
        $upload = new \Think\Upload();
        $upload -> maxSize = 3145728;
        $upload -> exts = array("jpg","jpeg","png","gif");
        $upload -> autoSub = false;
        $upload -> rootPath = "./Public/";
        $upload -> savePath = "uploads/";
        if($image = $upload -> upload()){
            $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
        }else{
            $this -> error("error1");
        }
        if(M("server") -> add($info)){
            $this -> success("success!",U("index"));
        }else{
            $this -> error("error2");
        }
    }
    
    public function modServer(){
        $id = I("id");
        $server = M("server") -> find($id);
        $this -> assign("server",$server);
        $this -> display();
    }
    
    public function doModServer(){
        $info["name"] = I("name");
        $info["url"] = I("url");
        $upload = new \Think\Upload();
        $upload -> maxSize = 3145728;
        $upload -> exts = array("jpg","jpeg","png","gif");
        $upload -> autoSub = false;
        $upload -> rootPath = "./Public/";
        $upload -> savePath = "uploads/";
        if($image = $upload -> upload()){
            $info["image"] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
        }

        $oldImage = I("image");
        $where['id'] = I("id");
        if(M("server") -> where($where) -> save($info)){
            unlink($oldImage);
            $this -> success("success",U("index"));
        }
    }
    
    public function delServer(){
        $id = I("id");
        $where["id"] = $id;
        if(M("server") -> where($where) -> delete()){
            $this -> success("删除成功",U("index"));
        }else{
            $this -> error("删除失败");
        }
    }
}
